import request from '@utils/request.js';
import * as actionType from './actionType.js';
import {
    message,Modal
} from 'antd';
//登陆
export const login = (values) => {
      return (dispatch) => {
          request('POST', '/api/server/web/rest/user/login', {
              cmd: "login",
              type: "request",
              request: {
                  ...values
              }
          }).then(obj => {
              let res = obj.response;
              if (res.res) {
                  message.success('登录成功', 1);
                  sessionStorage.setItem('islogin', JSON.stringify(res.res));
                    //  判断角色状态
                sessionStorage.setItem("reloId",res.roleId);

                if(res.roleId === 1) {
                    window.location.href = '/AccountConfig'
                } else if(res.roleId === 2) {
                    window.location.href = '/MyBoss/DatStatistics'
                } else if(res.roleId === 3){
                    window.location.href = '/PersonalCenter'
                } 
              } else{
                  if (res.exception == null) {
                      message.error("该账号为管理员账号，未授权！");

                  } else {
                      message.error(res.exception);
                  }
              }
          }).catch(err => console.log(err));
      }
  }
// 登录相关
const loginRequest = result => ({
    type: actionType.LOGIN,
    result
});
//注册账号
export const registeredRequest = (values) => {
      return (dispatch) => {
          request('POST', '/api/server/web/rest/user/registered', {
              cmd: "registered",
              type: "request",
              request: {
                  ...values
              }
          }).then(obj => {
              let res = obj.response;
              if (res.res) {
                  if (values.role == "Admin"){
                      message.success('注册成功,请等待该机管理员授权！', 1);
                  }else{
                      message.success("注册成功！")
                  }

              } else {
                  message.error('注册失败' + res.exception, 1);
              }
          }).catch(err => console.log(err));
      }
  }
  //找回密码
export const editPassword = (values) => {
    return (dispatch) => {
        request('POST', '/api/server/web/rest/user/editPassword', {
            cmd: "editPassword",
            type: "request",
            request: {
                ...values
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                message.success('重置成功', 1);
            } else {
                message.error('重置失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}
//修改密码
export const alterPassword = (values) => {
    return (dispatch) => {
        request('POST', '/api/server/web/rest/user/alterPassword', {
            cmd: "alterPassword",
            type: "request",
            request: {
                ...values
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                message.success('重置成功', 1);
            } else {
                message.error(res.exception);
            }
        }).catch(err => console.log(err));
    }
}
export const isloginRequest = () => {
    return (dispatch) => {
        request("POST", '/api/server/web/rest/user/isLogin', {
            cmd: "isLogin",
            type: "request",
            request: {

            }
        }).then(response => {
            const result = response.response;
            if (result.res) {
                sessionStorage.setItem(
                    "islogin",
                    JSON.stringify(result.res)
                );
                console.log(result.res, 'res')
            } else {
                sessionStorage.setItem("islogin", JSON.stringify(false));
                sessionStorage.clear();
                window.location.href = '/login'
            }

        }).catch(error => {
            console.log(error)
        });
    };
};
//注销登录
export const logoutRequest = (params) => {
      return (dispatch) => {
          request("POST", '/api/server/web/rest/user/logout', {
              cmd: "logout",
              type: "request",
              request: {
  
              }
          }).then(response => {
              const result = response.response;
              if (result.res) {
                //   message.success('账号注销成功', 1);
                  sessionStorage.clear();
                  dispatch(logoutReducer(result.message))
              } else {
                  message.error('账号注销失败' + result.exception, 1.5);
              }
          }).catch(error => {
              console.log(error)
          });
      };
  };
  
  //账号注销
  const logoutReducer = (result) => ({
      type: actionType.LOGOUT,
      result
  })

